package com.example.firstspringboot.controller;

import com.example.firstspringboot.bean.User;
import com.example.firstspringboot.utils.MyResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/v1/user")
public class UserController1 {
    private static Map<Long, User> users;

    static {
        users = new HashMap<>();
        users.put(1L, new User(1L, "zwz", 20));
    }

    /**
     * <p>GET请求，得到所有 user</p>
     * <p>url(Get): <code>localhost:8080/v1/user/</code></p>
     */
    @GetMapping("/")
    public MyResult<List<User>> getAllUser() {
        MyResult<List<User>> result = new MyResult<>();

        List<User> list = new ArrayList<>(users.values());
        result.setData(list);
        result.setResultCode(MyResult.OK);
        result.setMessage("查询成功");

        return result;
    }

    /**
     * <p>GET请求，根据 id 获得 user</p>
     * <p>url(Get): <code>localhost:8080/v1/user/1</code></p>
     */
    @GetMapping("/{id}")
    public MyResult<User> getUserById(@PathVariable("id") Long id) {
        MyResult<User> result = new MyResult<>();

        User user = users.getOrDefault(id, null);
        result.setData(user);
        if (user == null) {
            result.setResultCode(MyResult.ERROR);
            result.setMessage("查询失败，不存在该 id 对应的 user");
        } else {
            result.setResultCode(MyResult.OK);
            result.setMessage("查询成功");
        }

        return result;
    }

    /**
     * <p>POST请求，添加一个 user</p>
     * <p>url(Post): <code>localhost:8080/v1/user/</code></p>
     * <p>body</p>
     * <pre>
     * {
     *     "id": 1,
     *     "name": "zwz"
     *     "age": "20"
     * }
     * </pre>
     */
    @PostMapping("/")
    public MyResult<Boolean> addUser(@RequestBody User user) {
        MyResult<Boolean> result = new MyResult<>();

        Long id = user.getId();
        if (id == null) {
            result.setData(false);
            result.setResultCode(MyResult.ERROR);
            result.setMessage("id为空，添加失败");
        } else if (users.containsKey(id)) {
            result.setData(false);
            result.setResultCode(MyResult.ERROR);
            result.setMessage("id重复，添加失败");
        } else {
            result.setData(true);
            result.setResultCode(MyResult.OK);
            result.setMessage("添加成功");
            users.put(id, user);
        }

        return result;
    }

    /**
     * <p>PUT请求，用来更新 User 信息（ id 值一般不做修改）</p>
     * <p>url(Put): <code>localhost:8080/v1/user/</code></p>
     */
    @PutMapping("/")
    public MyResult<Boolean> updateUser(@RequestBody User user) {
        MyResult<Boolean> result = new MyResult<>();

        Long id = user.getId();
        if (users.containsKey(id)) {
            result.setData(true);
            result.setResultCode(MyResult.OK);
            result.setMessage("更新成功");
            users.put(id, user);
        } else {
            result.setData(false);
            result.setResultCode(MyResult.ERROR);
            result.setMessage("id不存在，更新失败");
        }

        return result;
    }

    /**
     * <p>DELETE请求，用来删除User</p>
     * <p>url(Put): <code>localhost:8080/v1/user/1</code></p>
     */
    @DeleteMapping("/{id}")
    public MyResult<Boolean> deleteUser(@PathVariable("id") Long id) {
        MyResult<Boolean> result = new MyResult<>();

        if (users.containsKey(id)) {
            users.remove(id);
            result.setData(true);
            result.setResultCode(MyResult.OK);
            result.setMessage("删除成功");
        } else {
            result.setData(false);
            result.setResultCode(MyResult.ERROR);
            result.setMessage("id不存在，删除失败");
        }
        return result;
    }


}
